﻿
/**
 *  
 *  Author: H. James de St. Germain
 *  Commented by: ________ your name ________
 *  
 *  You must fully comment this program as if you had written it.
 *  Every function must have a header.  Add any inline comments that you
 *  think are necessary to explain "subtle" details.
 *
 *  Remove this comment at the top and replace it with a header comment
 *  describing the overall workings of this object.  (Keep my name and
 *  your name above).
 */

package
{
  import flash.display.Sprite;
  import flash.events.MouseEvent;
  import flash.events.Event;
  import flash.text.TextField;
  import flash.events.FocusEvent;
  import flash.text.TextFormat;
        
  public class Button extends Sprite
  {
      private var title : String;
      private var value : int = 0;
                
      public function Button( my_title:String, my_id:int, my_width :int, my_height :int, call_back : Function) 
        {
          this.title  = my_title;
          this.value  = my_id;

          this.addEventListener(MouseEvent.MOUSE_DOWN, create_mouse_down_display_list); 
          this.addEventListener(MouseEvent.MOUSE_UP,   create_mouse_over_display_list); 
          this.addEventListener(MouseEvent.ROLL_OVER,  create_mouse_over_display_list);
          this.addEventListener(MouseEvent.ROLL_OUT,   create_default_display_list);    
          this.addEventListener(MouseEvent.CLICK,      call_back);      
                        
          this.create_default_display_list( null );
          this.create_title_display_list();
                        
          this.scaleX = my_width / 100;
          this.scaleY = my_height / 50;
        }

      private function create_default_display_list( e:MouseEvent ) : void
        {
          this.graphics.clear();
          this.graphics.lineStyle(2, 0xCCCCCC);
                        
          this.graphics.beginFill( 0xCDCDCD );
          this.graphics.drawRect(0, 0, 100, 40);
          this.graphics.endFill();
        }
                
      private function create_mouse_down_display_list( e:MouseEvent ) : void
        {
          this.graphics.clear();
          this.graphics.lineStyle(2, 0xCCCCCC);
                        
          this.graphics.beginFill(0xCDCDCD);
          this.graphics.drawRect(0, 0, 100, 40);
          this.graphics.endFill();
        }
                
      private function create_mouse_over_display_list( e:MouseEvent ) : void
        {
          this.graphics.clear();
          this.graphics.lineStyle(2, 0x000000);

                        
          this.graphics.beginFill(0xAAAAAA);
          this.graphics.drawRect(0, 0, 100, 40);
          this.graphics.endFill();
        }
                
                
      private function create_title_display_list( ) : void
        {
          var text:TextField = new TextField();
          var my_format : TextFormat = new TextFormat();

          text.width = this.width;
          text.height = this.height;

          my_format.align= "center";
          my_format.font = "Arial";
          my_format.size = 18;
          my_format.color = 0xFFFFFF;

          text.text = this.title;
          text.selectable = false;
          text.antiAliasType = "advanced";
          text.setTextFormat( my_format );
          text.y = 7.5; // center vertically

          this.addChild(text);
        }
                
      public function set_value( the_value : int ) : void
        {
          this.value = the_value;
        }

      public function get_value( ) : int
        {
          return this.value;
        }


  }
}












